﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.IManager;
using Business.Manager;
using Entity;
using Utility;
using System.Data;
using System.Collections;
namespace OnlinePayment.OP
{
    public partial class OrderConfirmView : System.Web.UI.Page
    {
        public Order order = new Order();
        String goodsId = String.Empty;
        IDepartmentManager departmentManager = new DepartmentManager();
        IPaymentItemManager paymentItemManager = new PaymentItemManager();
        INetworkPortManager networkPortMangager = new NetworkPortManager();
        public List<ProjectOrder> orders;
        IOrderManager orderManager = new OrderManager();
        public decimal totalAmount = Convert.ToDecimal(0.00);
        public DataTable orderDataTable = new DataTable();
        public Student student = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                if (Session["Orders"] != null)
                {
                    orders = (List<ProjectOrder>)Session["Orders"];
                    //bindOrderData(orders);
                    initTuitionTable(orders);
                }
                else if (Session["Order"] != null)
                {
                    order = (Order)Session["Order"];
                    bindPaymentOrderData(order);
                }


                if (Session["User"] != null)
                {
                    student = (Student)Session["User"];
                }

            }



        }


        private void bindPaymentOrderData(Order order)
        {
            string type = order.PaymentType;
            lbl_PaymentProjectName.Text = type;
            lbl_totalAmount.Text = order.TotalAmount.ToString();
            if (type.Equals("一卡通"))
            {
                StudentCartHistoryOrder scOrder = (StudentCartHistoryOrder)order;
                lbl_StuCardId.Text = scOrder.stuCardId.ToString();
               
            }
            else if (type.Equals("水电费"))
            {
                UtilitiesHistoryOrder uhOrder = (UtilitiesHistoryOrder)order;

            }
            else if (type.Equals("网费"))
            {

            }
            else if (type.Equals("月网费"))
            {
            }
            else if (type.Equals("年网费"))
            {
            }
            else if (type.Equals("半年网费"))
            {
            }
        }

        


        

        private void initTuitionTable(List<ProjectOrder> orders)
        {
            int columnNum = 5;
            String[] headerStr = new String[columnNum];
            headerStr[0] = "年份";
            headerStr[1] = "详细项名称";
            headerStr[2] = "实缴金额";
            headerStr[3] = "应缴金额";
            headerStr[4] = "本次缴费金额";

            String[] dataFieldName = new String[columnNum];
            dataFieldName[0] = "year";
            dataFieldName[1] = "detailName";
            dataFieldName[2] = "amountShPay";
            dataFieldName[3] = "amountFcPay";
            dataFieldName[4] = "curPayAccount";

            Unit[] widths = new Unit[columnNum];
            widths[0] = new Unit("13%");
            widths[1] = new Unit("12%");
            widths[2] = new Unit("27%");
            widths[3] = new Unit("19%");
            widths[4] = new Unit("19%");

            GV_OrderConfirm.Columns.Clear();

            for (int i = 0; i < headerStr.Length; i++)
            {
                BoundField field = new BoundField();
                field.HeaderText = headerStr[i];
                field.DataField = dataFieldName[i];
                field.SortExpression = dataFieldName[i];
                field.ItemStyle.Width = widths[i];
                GV_OrderConfirm.Columns.Add(field);
            }


            orderDataTable.Columns.Add("year");
            orderDataTable.Columns.Add("detailName");
            orderDataTable.Columns.Add("amountShPay");
            orderDataTable.Columns.Add("amountFcPay");
            orderDataTable.Columns.Add("curPayAccount");
            DataRow row;
            foreach (ProjectOrder po in orders)
            {
                totalAmount += po.TotalAmount;
                List<OrderDetail> detailList = po.OrderDetail;
                foreach (OrderDetail od in detailList)
                {
                    /*About orderDataTable*/
                    int detailId = od.DetailId;
                    DataSet detailInfo = paymentItemManager.getStudentProjectDetailInfoByDetailId(detailId);
                    DataRow dr = detailInfo.Tables[0].Rows[0];

                    row = orderDataTable.NewRow();
                    row[0] = Convert.ToInt32(dr["year"]);
                    row[1] = dr["detailName"].ToString();
                    row[2] = Convert.ToDecimal(dr["amountShPay"]);
                    row[3] = Convert.ToDecimal(dr["amountFcPay"]);
                    row[4] = Convert.ToDecimal(od.Amount);
                    orderDataTable.Rows.Add(row);

                    /*About Session Order Detail*/
                    //od.ItemName = dr["detailName"].ToString();
                    //od.ProjectId = Convert.ToInt32(dr["stuProjectId"]);
                }
            }
            lbl_totalAmount.Text = totalAmount.ToString();


            GV_OrderConfirm.DataSource = orderDataTable;
            GV_OrderConfirm.DataBind();
            //Session["Orders"] = orders;
        }


        private void bindOrderData(List<ProjectOrder> orders)
        {
            foreach(ProjectOrder po in orders){
                totalAmount += po.TotalAmount;
                List<OrderDetail> detailList = po.OrderDetail;
                foreach (OrderDetail od in detailList)
                {
                    int detailId = od.DetailId;
                    DataSet detailInfo = paymentItemManager.getStudentProjectDetailInfoByDetailId(detailId);
                    DataRow dr = detailInfo.Tables[0].Rows[0];

                    //od.year = Convert.ToInt32(dr["year"]);
                    od.ItemName = dr["detailName"].ToString();
                }
            }

            lbl_totalAmount.Text = totalAmount.ToString();
        }


        protected void btn_AckPay_Click(object sender, EventArgs e)
        {

            BankPay bankPay = new BankPay();
            student = (Student)Session["User"];
            String orderId ="";
            bankPay.CurType = "人民币";
            bankPay.BankId = "1";//bankList.SelectedItem.Value;
            Department department;
            bankPay.Amount = Convert.ToDecimal(lbl_totalAmount.Text);
            bankPay.OrderDate = DateTime.Now;
            bankPay.PayAccount = "12312sadad";
            bankPay.BankId = Request.Form["img"];
            bankPay.Msg = "已付款";
            
            
            if (Session["Orders"] != null)
            {
                orderId = orderManager.getOrderId(String.Empty);
                bankPay.OrderId = orderId;
                orders = (List<ProjectOrder>)Session["Orders"];
                bankPay.GoodsName = "学费";
                bankPay.GoodsID = student.StudentId;
                department = departmentManager.getDepartmentInforByPaymentType(bankPay.GoodsName);
                bankPay.MerName = department.DepName;
                orderManager.generateOrder(bankPay, null, orders, null, student.StudentId);

            }
            else if (Session["Order"] != null)
            {
                order = (Order)Session["Order"];
                
                string type = order.PaymentType;
                orderId = orderManager.getOrderId(type);
                
                department = departmentManager.getDepartmentInforByPaymentType(type);
                bankPay.GoodsName = type;
                bankPay.MerName = department.DepName;
                
                switch (type)
                {
                    case "水电费":
                        UtilitiesHistoryOrder uhOrder = (UtilitiesHistoryOrder)order;
                        bankPay.OrderId = orderId;
                        bankPay.Msg = "未付款";
                        bankPay.Amount = Convert.ToDecimal(lbl_totalAmount.Text.ToString());
                        bankPay.OrderDate = DateTime.Now;
                        bankPay.BankId = "1";
                        bankPay.PayAccount = "xxxxx";
                        bankPay.GoodsID = uhOrder.UtilityId;
                        orderManager.generateOrder(bankPay, null, orders, null, student.StudentId);
                        break;
                    case "一卡通":
                        StudentCartHistoryOrder scOrder = (StudentCartHistoryOrder)order;
                        bankPay.GoodsID = scOrder.stuCardId;
                        bankPay.OrderId = orderId;
                        bankPay.BankId = "1";
                        orderManager.generateOrder(bankPay, null, orders, null, student.StudentId);
                        break;
                    default: //网费
                        bankPay.GoodsName = "网费";
                        department = departmentManager.getDepartmentInforByPaymentType(bankPay.GoodsName);
                        bankPay.MerName = department.DepName;
                        orderId = orderManager.getOrderId(bankPay.GoodsName);
                        bankPay.OrderId = orderId;
                        CampusNetworkHistoryOrder cnhOrder = (CampusNetworkHistoryOrder)order;
                        bankPay.GoodsID = cnhOrder.portId;
                        NetworkPort np = networkPortMangager.getNetworkPortInform(student.StudentId);
                        orderManager.generateOrder(bankPay, np, orders, null, student.StudentId);
                        break;
                }
            }

            //Session.Clear();
            Session["Orders"] = null;
            Session["Order"] = null;
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "showPayScript", "<script>showBox(" + orderId + ");</script>");

            //lbl_totalAmount.Text = totalAmount.ToString();

            //Response.Redirect("PayOrderView.aspx");
            //ClientScript.RegisterStartupScript(ClientScript.GetType(), "showPayScript", "<script>group_select("+toid+", "+toName+");</script>");
            //Response.Write("<script>alert('发表成功！');window.opener.location.href=window.opener.location.href;</script>");
//ClientScript.RegisterStartupScript(ClientScript.GetType(), "showPayScript", "<script>asyncbox.alert('sssss')});</script>");


            /*if (Session["Orders"] == null)
            {
                bankPay.Amount = order.TotalAmount;
                bankPay.GoodsName = order.PaymentType;
                bankPay.OrderId = order.OrderId;
                bankPay.OrderDate = order.OrderDate;
                department = departmentManager.getDepartmentInforByPaymentType(order.PaymentType);
                bankPay.MerAcct = department.RecvAccount;
                bankPay.MerName = department.DepName;
                bankPay.GoodsID = goodsId;

            }
            else
            {
                bankPay.Amount = totalAmount;
                bankPay.GoodsName = orders[0].PaymentType;
                bankPay.OrderId = orderId;
                bankPay.OrderDate = DateTime.Now;
                department = departmentManager.getDepartmentInforByPaymentType(orders[0].PaymentType);
*/
            //}
            /*bankPay.MerAcct = department.RecvAccount;
            bankPay.MerName = department.DepName;
            Session["BankPay"] = bankPay;
            String s = Session["BankPay"].ToString();
            Response.Redirect("/Bank/ConstructBank.aspx");*/

        }




    }
}